import csv
import os

classDicList=list()
with open('birdsong_metadata.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        classDic=dict()
        classDic['file_id']=row['file_id']
        classDic['species']=row['species']
        classDicList.append(classDic)

        if(os.path.exists('clustered/'+row['species'])==False):
            os.mkdir('clustered/'+row['species'])

g = os.walk('songs')

for path, dir_list, file_list in g:
    for file_name in file_list:
        for classDic in classDicList:
            if(file_name[2:-5]==classDic['file_id']):
                os.rename(path+'/'+file_name,'clustered/'+classDic['species']+'/'+file_name)